import globalMethods from "@/store/$u.mixin.js";
import cameraAndAudio from "./newcarmera.js";
import trtc from "./trtc.js";
import map from "./map.js";

import {
  login,
  user_info,
  friendList,
  app_config,
  messageUnreadNum,
  advert,
  friends_sos,
  messageCategory,
  getIntroduce,
} from "@/common/api/index.js";

module.exports = {
  mixins: [globalMethods, cameraAndAudio, trtc, map],
  data() {
    return {};
  },
  onLoad() {},
  onShow() {},
  async created() {
    if (!uni.getStorageInfoSync("token")) {
      return;
    }

    // await this.goLogin();
    await this.getUserInfo();
    await this.getAppInfo();
    await this.$store.dispatch("openMqtt");
    await this.get_friendList();
    await this.get_getUserSign();
    await this.openLocation();
    await this.onListenerLocation();
    await this.onShowCloseRoom();
    await this.get_unreadNum();
    // await this.checkToken();
  },
  methods: {
    async goLogin() {
      let brand = uni.getSystemInfoSync().brand;
      let account = "";
      console.log(brand);
      if (brand == "xiaomi") {
        account = 18111726632;
      } else {
        account = 18283378923;
      }

      let res = await login({
        account: account,
        scene: 2,
        terminal: 6,
        password: "",
        code: 8888,
      });
      if (res.code == 1) {
        uni.setStorageSync("token", res.data.token);
      } else {
        this.showMsg(res.msg);
      }
    },
    async getUserInfo() {
      let res = await user_info();
      if (res.code == 1) {
        this.$store.commit("saveUserInfo", res.data);
        console.log("saveUserInfo", uni.getStorageSync("token"));
      } else {
        this.showMsg(res.msg);
      }
    },
    async getAppInfo() {
      let res = await app_config();
      if (res.code == 1) {
        console.log(res.data);
        this.toVuex("appData", res.data);
      } else {
        this.showMsg(res.msg);
      }
    },
    async get_friendList() {
      let res = await friendList();
      console.log("9999999999999", res);
      if (res.code == 1) {
        let id = this.$store.state.userInfo.id;
        console.log(id, this.$store.state);
        let subArr = res.data.map((item) => {
          return `${item}_${id}`;
        });
        subArr.push(`system_user_${id}`);
        this.toVuex("subscribeArr", subArr);
        this.$store.commit("subscribeTopic");
      } else {
        this.showMsg(res.msg);
      }
    },
    successSendMqt(msg, topic) {
      let publishTopic = getpublishTopic(topic);
      this.$store.commit("publishMessage", {
        topic: publishTopic,
        msg: JSON.stringify({
          code: 1,
          time: new Date().getTime(),
          msg: "ok",
          todo: msg,
        }),
      });
    },
    // 获取未读消息
    async get_unreadNum() {
      let res = await messageUnreadNum();
      if (res.code == 1) {
        this.$store.commit("setMessageValue", res.data.unread_count);
        if (res.data.unread_count) {
          uni.setTabBarBadge({
            index: 2,
            text: `${res.data.unread_count}`,
          });
        } else {
          uni.removeTabBarBadge({
            index: 2,
          });
        }
      } else {
        this.showMsg(res.msg);
      }
    },
    // 获取轮播
    async banner(space) {
      let res = await advert({
        space: space,
      });
      if (res.code == 1) {
        return res.data;
      } else {
        this.showMsg(res.msg);
      }
    },
    async handleHelp() {
      console.log("sos请求", this.addressInfo);
      let res = await friends_sos({
        ...this.addressInfo,
      });
      this.showMsg(res.msg);
    },
    async checkToken() {
      // 在这检查token是否拥有   更新
    },
    async getIntroduce() {
      let res = await getIntroduce();
      if (res.code == 1) {
        return res.data.course;
      } else {
        this.showMsg(res.msg);
      }
    },
  },
};

function getpublishTopic(topic) {
  let arr = topic.split("_").reverse();
  return arr.join("_");
}
